package ecom.beans;

import ecom.beans.EuroConvertorBean.Currency;
import ecom.beans.dao.AccountDAO;
import ecom.beans.dao.UserDAO;
import ecom.beans.exception.AccountBeanException;
import ecom.beans.exception.CartException;
import ecom.beans.exception.DeviseException;
import ecom.beans.exception.NotAccountAssociatedToUser;
import ecom.beans.exception.NotLoginException;
import ecom.beans.exception.StockQuantityUnavailable;
import java.io.Serializable;
import javax.ejb.Stateful;
import java.util.Set;
import java.util.List;
import javax.ejb.EJB;

@Stateful
public class EcomCustomerBean extends EcomAdminUserBean implements EcomCustomerRemote, Serializable {

    @EJB
    private CartLocal cart;
    private UserBean user;
    @EJB
    private EuroConvertorLocal convertor;

    public EcomCustomerBean() {
    }

    @Override
    public void setUser(UserBean user) {
        this.user = user;
        this.cart.initializeCart();
        this.cart.setState(CartBean.CartState.EMPTY);
    }

    @Override
    public void addProductToCart(Long idProduct, Long idProductstore, Integer quantity) throws StockQuantityUnavailable, CartException, NotAccountAssociatedToUser, NotLoginException {
        if (user == null) {
            throw new NotLoginException("To add products to your cart, you have to login");
        } else {
            if (user.getAccount() == null) {
                throw new NotAccountAssociatedToUser("You have to associate an account with your user account");
            } else {
                cart.addProduct(user.getAccount(), idProduct, idProductstore, quantity);
            }
        }

    }

    @Override
    public void removeProductFromCart(Long idProduct, Long idProductstore) throws CartException {
        cart.removeProduct(idProduct, idProductstore);
    }

    @Override
    public void updateQuantityProduct(Long idProduct, Long idProductstore, Integer updateQuantity) throws StockQuantityUnavailable, CartException {
        cart.updateProductQuantity(idProduct, idProductstore, updateQuantity);
    }

    @Override
    public Set<PurchaseBean> getPurchasesInCart() {
        return cart.getCart();
    }

    @Override
    public void buyCart() throws NotLoginException, NotAccountAssociatedToUser, CartException, AccountBeanException {
        if (user == null) {
            throw new NotLoginException("To do your purchases, you have to login");
        } else {
            if (user.getAccount() == null) {
                throw new NotAccountAssociatedToUser("You have to associate an account with your user account");
            } else {
                cart.buy(user.getAccount());
            }
        }
    }

    @Override
    public Double getCartTotalPriceInEuro() {
        return cart.getTotalPrice();
    }

    @Override
    public Double getCartTotalPriceInCurrentDevise(String currentCurrency) throws DeviseException {
        return convertor.convertFromEuro(cart == null ? new Double(0) : cart.getTotalPrice(), currentCurrency);
    }

    @Override
    public CartBean.CartState getCartState() {
        return cart.getState();
    }

    @Override
    public List<String> getCurrencySymbols() {
        return convertor.getCurrencySymbols();
    }

    @Override
    public String getCurrencyRealName(String valueOf) throws DeviseException {
        return convertor.getCurrencyRealName(valueOf);
    }

    @Override
    public Currency getCurrencyByName(String name) throws DeviseException {
        return Currency.get(name);
    }

    @Override
    public Double convertFromEuro(Double amount, String currentCurrency) throws DeviseException {
        return convertor.convertFromEuro(amount, currentCurrency);
    }

    @Override
    public Double convertToEuro(Double amount, String currentCurrency) throws DeviseException {
        return convertor.convertToEuro(amount, currentCurrency);
    }

    @Override
    public void deposit(Double amount) throws NotLoginException, NotAccountAssociatedToUser {
        if (user == null) {
            throw new NotLoginException("To deposit money on your account, you have to login");
        } else {
            if (user.getAccount() == null) {
                throw new NotAccountAssociatedToUser("No account associated with your user account");
            } else {
                user.getAccount().deposit(amount);
                AccountDAO.updateAccount(user.getAccount());
            }
        }
    }

    @Override
    public CartBean getCart() {
        return (CartBean) cart;
    }

    @Override
    public void setCart(CartBean cart) {
        this.cart = cart;
    }

    @Override
    public UserBean getUser() {
        return (UserBean) user;
    }

    public void updateUser(String pseudo, String password, String email, String address, String postcode, String city, String country,
            String currency, String telephone) {
        user.setPseudo(pseudo);
        user.setPassword(password);
        user.setEmail(email);
        user.setAdress(address);
        user.setPostalCode(postcode);
        user.setCity(city);
        user.setCountry(country);
        user.setCurrency(currency);
        user.setCurrency(telephone);
        UserDAO.updateUser(user);
    }
}
